/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.timesoft.zk.common.ctrl;

import com.timesoft.common.bean.ResponseCommon;
import com.timesoft.common.convertor.Base64ConvertorUtils;
import com.timesoft.global.webservice.WebServiceClient;
import com.timesoft.hibernate.bean.SettingFormatDate;
import com.timesoft.hibernate.bean.SettingFormatNumber;
import com.timesoft.webservice.client.setting.SettingWebService;
import com.timesoft.zk.common.LocaleType;
import com.timesoft.zk.common.ResourceUtil;
import com.timesoft.zk.common.message.AlertMessages;
import java.util.Locale;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.zkoss.util.Locales;
import org.zkoss.web.Attributes;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.util.GenericAutowireComposer;

/**
 *
 * @author sorasaks
 */
public abstract class MainPageWsController extends GenericAutowireComposer<Component> {

    private static final Logger LOG = Logger.getLogger(MainPageWsController.class);

    private final SettingWebService settingService = WebServiceClient.getSettingService();
    private final Base64ConvertorUtils base64 = new Base64ConvertorUtils();

    public static final String DATA_OBJECT_EXECUTION = "DATA_OBJECT_EXECUTION";

    public static final String ATTRIBUTE_CONTEXT_PATH = "contextPath";
    public static final String ATTRIBUTE_REAL_PATH = "realPath";
    public static final String ATTRIBUTE_AMOUNT_FORMAT = "amountFormat";
    public static final String ATTRIBUTE_DATE_FORMAT = "dateFormat";

    public static final String ATTRIBUTE_POPUP_INCLUDE = "popupInc";
    public static final String ATTRIBUTE_SIGN_IN_INCLUDE = "signinInc";
    public static final String ATTRIBUTE_SETTING_INCLUDE = "settingInc";
    public static final String ATTRIBUTE_HEADER_INCLUDE = "headerInc";
    public static final String ATTRIBUTE_CONTENT_INCLUDE = "contentInc";
    public static final String ATTRIBUTE_FOOTER_INCLUDE = "footerInc";

    public static final String ATTRIBUTE_CUSTOMER_CODE = "customerCode";
    public static final String ATTRIBUTE_AUTHENTIFICATION = "authentification";

    protected String pageName;
    protected Locale locale;
    protected String contextPath;
    protected String realPath;
    protected String amountFormat;
    protected String dateFormat;
    protected Boolean isResource;

    protected String customerCode;
    protected Boolean authentification;

    public MainPageWsController() throws Exception {
        this.pageName = "";
        this.isResource = Boolean.FALSE;
        initialParameter();
        LOG.info("::::::::::: pageName :::::::::::::" + pageName);
        LOG.info("::::::::::: locale :::::::::::::" + locale);
        LOG.info("::::::::::: contextPath :::::::::::::" + contextPath);
        LOG.info("::::::::::: realPath :::::::::::::" + realPath);
        LOG.info("::::::::::: amountFormat :::::::::::::" + amountFormat);
        LOG.info("::::::::::: dateFormat :::::::::::::" + dateFormat);
    }

    public MainPageWsController(String _pageName, Boolean _isResource) throws Exception {
        this.pageName = _pageName;
        this.isResource = _isResource;
        initialParameter();
        LOG.info("::::::::::: pageName :::::::::::::" + pageName);
        LOG.info("::::::::::: locale :::::::::::::" + locale);
        LOG.info("::::::::::: contextPath :::::::::::::" + contextPath);
        LOG.info("::::::::::: realPath :::::::::::::" + realPath);
        LOG.info("::::::::::: amountFormat :::::::::::::" + amountFormat);
        LOG.info("::::::::::: dateFormat :::::::::::::" + dateFormat);
    }

    @Override
    public void doBeforeComposeChildren(Component comp) throws Exception {
        super.doBeforeComposeChildren(comp); //To change body of generated methods, choose Tools | Templates.
        if (!StringUtils.isEmpty(pageName)) {
            if (isResource) {
                setTitle(ResourceUtil.getLabel(pageName, locale));
            } else {
                setTitle(pageName);
            }
        }
    }

    protected void setTitle(String _pageName) {
        getPage().setTitle(_pageName);
    }

    private void initialParameter() throws Exception {
        setParameter();
        reloadParameter();
    }

    private void setLocale() {
        Object obj = Executions.getCurrent().getSession().getAttribute(Attributes.PREFERRED_LOCALE);
        Locale localeObj = Locales.getCurrent();
        if (obj == null) {
            Executions.getCurrent().getSession().setAttribute(Attributes.PREFERRED_LOCALE, Locales.getCurrent());
        } else {
            localeObj = (Locale) obj;
        }
        this.locale = (localeObj == null ? LocaleType.US : localeObj);
    }

    private void setRealPath() {
        this.realPath = Executions.getCurrent().getScheme() + "://" + Executions.getCurrent().getServerName()
                + ":" + Executions.getCurrent().getServerPort();
    }

    private void setContextPath() {
        this.contextPath = Executions.getCurrent().getScheme() + "://" + Executions.getCurrent().getServerName()
                + ":" + Executions.getCurrent().getServerPort() + Executions.getCurrent().getContextPath();
    }

    private void setAmountFormat() throws Exception {
        ResponseCommon common = (ResponseCommon) base64.stringToGenericObj(settingService.getSettingFormatNumber());
        if (common.isFlag()) {
            SettingFormatNumber setting = (SettingFormatNumber) base64.stringToGenericObj(common.getValue());
            this.amountFormat = setting.getFormat();
        } else {
            LOG.debug(":::::::::::::: setting ::::::::::::::::" + common);
            AlertMessages.alertMessage(common.getMessage());
        }
    }

    private void setDateFormat() throws Exception {
        ResponseCommon common = (ResponseCommon) base64.stringToGenericObj(settingService.getSettingFormatDate());
        if (common.isFlag()) {
            SettingFormatDate setting = (SettingFormatDate) base64.stringToGenericObj(common.getValue());
            this.dateFormat = setting.getFormat();
        } else {
            LOG.debug(":::::::::::::: setting ::::::::::::::::" + common);
            AlertMessages.alertMessage(common.getMessage());
        }
    }

    private void reloadParameter() throws Exception {
        if (locale == null) {
            setLocale();
            Executions.getCurrent().getSession().setAttribute(Attributes.PREFERRED_LOCALE, locale);
        }
        if (realPath == null) {
            setRealPath();
            Executions.getCurrent().getSession().setAttribute(MainPageWsController.ATTRIBUTE_REAL_PATH, realPath);
        }
        if (contextPath == null) {
            setContextPath();
            Executions.getCurrent().getSession().setAttribute(MainPageWsController.ATTRIBUTE_CONTEXT_PATH, contextPath);
        }
        if (dateFormat == null) {
            setDateFormat();
            Executions.getCurrent().getSession().setAttribute(MainPageWsController.ATTRIBUTE_DATE_FORMAT, dateFormat);
        }
        if (amountFormat == null) {
            setAmountFormat();
            Executions.getCurrent().getSession().setAttribute(MainPageWsController.ATTRIBUTE_AMOUNT_FORMAT, amountFormat);
        }
    }

    private void setParameter() throws Exception {
        locale = (Locale) Executions.getCurrent().getSession().getAttribute(Attributes.PREFERRED_LOCALE);
        realPath = (String) Executions.getCurrent().getSession().getAttribute(MainPageWsController.ATTRIBUTE_REAL_PATH);
        contextPath = (String) Executions.getCurrent().getSession().getAttribute(MainPageWsController.ATTRIBUTE_CONTEXT_PATH);
        dateFormat = (String) Executions.getCurrent().getSession().getAttribute(MainPageWsController.ATTRIBUTE_DATE_FORMAT);
        amountFormat = (String) Executions.getCurrent().getSession().getAttribute(MainPageWsController.ATTRIBUTE_AMOUNT_FORMAT);
        customerCode = (String) Executions.getCurrent().getSession().getAttribute(MainPageWsController.ATTRIBUTE_CUSTOMER_CODE);
        authentification = (Boolean) Executions.getCurrent().getSession().getAttribute(MainPageWsController.ATTRIBUTE_AUTHENTIFICATION);
    }

    protected abstract void initialIncludeComponent();

}
